<!--
 * @Author: your name
 * @Date: 2021-01-19 16:55:57
 * @LastEditTime: 2021-01-20 15:51:27
 * @LastEditors: Please set LastEditors
 * @Description: 自定义档案
 * @FilePath: \html\src\components\flowset\Formcreated\components\business\customarchives.vue
-->
<template>
  <div>
    <el-cascader
      ref="cascarderRef"
      v-model="cascarderData"
      :disabled="disabled"
      :options="optionData"
      :props="cascaderProps"
      clearable
      :show-all-levels="false"
      @change="handleCascarderChange"
    />

  </div>
</template>
<script>
import { getCustomArchivesList } from '@/api/customarchives/customarchives'
import { getListAndTree } from '@/api/customarchives/customarchivesdetail'
export default {
  props: {
    data: {
      type: Object,
      default: () => { }
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      cascarderData: '',
      cascaderProps: {
        label: 'name', // 显示的选项
        value: 'Id', // 选择的value值
        children: 'Children', // 子级属性名
        checkStrictly: true, // 可以选择任意一级
        emitPath: false,
        lazy: true,
        lazyLoad(node, resolve) {
          setTimeout(() => {
            resolve(node)
          }, 500)
        }
      },
      queryParams: {
        ArchivesId: '',
        IsUse: true
      },
      optionData: []
    }
  },
  created() {
    this.getCustomArchivesData()
  },
  mounted() {
    this.backData()
  },
  methods: {
    handleCascarderChange(val) {
      if (this.optionData.length > 0) {
        var arr = this.$refs.cascarderRef.getCheckedNodes(true)[0].data
        this.$emit('update:data', { Id: arr.Id, name: arr.name })
      }
    },
    backData() {
      if (this.data !== undefined) {
        var archivesData = this.data
        if (archivesData.Id === undefined || archivesData.Id === '' || archivesData.Id === null) { return }
        this.cascarderData = archivesData.Id
      }
    },
    getCustomArchivesData() {
      var _this = this
      getCustomArchivesList({}).then(res => {
        if (res.Data.length > 0) {
          for (let index = 0; index < res.Data.length; index++) {
            _this.optionData.push({ Id: res.Data[index].Id, name: res.Data[index].ArchivesName, Children: [], disabled: true })
            _this.queryParams.ArchivesId = res.Data[index].Id
            getListAndTree(_this.queryParams).then(res1 => {
              _this.optionData[index].Children = res1.Data
            })
          }
        }
      })
    }
  }
}
</script>
